import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.util.Arrays;

public class TesArff {
	static int mode;
	static String PATH_WEKA;
	static int ROWS;
	static String PATH_INPUT = "../tes";
	static byte[][] input;
	static int COLS;
	static File[] listOfFiles;

	public static void main(String[] args) throws Exception {
		if (args.length > 0) {
			mode = Integer.valueOf(args[0]);
		} else {
			mode = 0;
		}
		PATH_WEKA = "../tes" + mode + ".arff";

		readInputs();
		writeArff();
	}

	private static void readInputs() throws Exception {
		File folder = new File(PATH_INPUT);
		listOfFiles = folder.listFiles();
		Arrays.sort(listOfFiles);
		COLS = listOfFiles.length;
		ROWS = (int) listOfFiles[0].length();
		input = new byte[COLS][ROWS];

		for (int c = 0; c < COLS; c++) {
			if (listOfFiles[c].isFile()) {
				FileInputStream fis = new FileInputStream(listOfFiles[c]);
				fis.read(input[c]);
			}
		}
	}

	private static void writeArff() throws Exception {
		FileWriter fw = new FileWriter(PATH_WEKA);
		BufferedWriter bw = new BufferedWriter(fw);

		bw.write("@relation r\n");
		for (int c = 0; c < COLS; c++) {
			String fileName = listOfFiles[c].getName();
			String attName = fileName.substring(0, 5);
			bw.write("@attribute " + attName + " numeric\n");
		}

		if (mode < 2) {
			bw.write("@attribute label numeric\n");
		} else {
			bw.write("@attribute label "
					+ "{0,10,20,30,40,50,60,70,80,90,100}\n");
		}
		bw.write("@data\n");

		for (int r = 0; r < ROWS; r++) {
			for (int c = 0; c < COLS; c++) {
				bw.write(input[c][r] + ",");
			}
			bw.write("0\n");
		}

		bw.close();
	}
}
